import { ZYSDK } from "../../../ZYSDK";
import gameModel from "../model/gameModel";

/**
 * 
 * @author: xj.huang
 * @date: 2021/08/11 14:26:35
 */
class sound {
    private _isMuteSound = false;
    private _isMuteMusic = false;
    private _curPlayingBgmName: string;
    constructor() {

    }
    private static _instance: sound;
    public static get instance(): sound {
        if (!this._instance) {
            this._instance = new sound();
        }
        return this._instance;
    }
    /**
     * 是否静音
     * @emits "isMute" (赋值成功后会派发事件，并带上IEventPayload作为参数)
     */
    public get isMuteSound(): boolean {
        return this._isMuteSound;
    }
    public set isMuteSound($value: boolean) {
        this._isMuteSound = $value;
        gameModel.userData.hasSound = this._isMuteSound
        // Laya.SoundManager.soundMuted=$value
    }

    /**
     * 是否静音
     * @emits "isMute" (赋值成功后会派发事件，并带上IEventPayload作为参数)
     */
    public get isMuteMusic(): boolean {
        return this._isMuteMusic;
    }
    public set isMuteMusic($value: boolean) {
        this._isMuteMusic = $value;
        Laya.SoundManager.musicMuted = this._isMuteMusic
        gameModel.userData.hasMusic = this._isMuteMusic
    }

    /**
     * 播放游戏统一的背景音乐
     */
    public playBgm($isStopFirst = false) {
        if ($isStopFirst) this.stopSound(ZYSDK.config.sound.bgm);
        this.playMusic(ZYSDK.config.sound.bgm);
    }
    public stopBgm($isStopFirst = false) {
        Laya.SoundManager.stopMusic();
    }

    /** 
     * 播放背景音乐（ps需要注意的是播放音效）
     * @param $musicName 文件名，不需要扩展名，只支持mp3
     **/
    public playMusic($musicName: string): void {
        if (!ZYSDK.hasDeployed || this.isMuteMusic || !$musicName) return;
        const bgmName = ZYSDK.config.sound.bgm;
        if (this._curPlayingBgmName !== bgmName) {
            this._curPlayingBgmName = bgmName;
        } else {
            return;
        }
        const url = this.cookSoundUrlByName($musicName);
        Laya.SoundManager.playMusic(url);
        
    }

    /**
     * 播放音效
     * @param $effectName 文件名，不需要扩展名，只支持mp3
     * @param $loop  循环次数,0表示无限循环。(默认1)
     */
    public playSoundEffect($effectName: string, $loop = 1): void {
        if (!ZYSDK.hasDeployed || this.isMuteSound || !$effectName) return;
        const url = this.cookSoundUrlByName($effectName);
        Laya.SoundManager.playSound(url, $loop);
    }

    /**
    * 停止播放音效。此方法能够停止任意声音的播放（包括背景音乐和音效）
    * @param $effectName 文件名，不需要扩展名，只支持mp3
    */
    public stopSound($effectName: string) {
        if (!ZYSDK.hasDeployed ||this.isMuteSound|| !$effectName) return;
        const isBgm = $effectName === ZYSDK.config.sound.bgm;
        if (isBgm) {
            this._curPlayingBgmName = undefined;
        }
        const url = this.cookSoundUrlByName($effectName);
        if (isBgm) {
            Laya.SoundManager.stopMusic();
        } else {
            Laya.SoundManager.stopSound(url);
        }
    }

    /** 全部音效停止，包括背景音乐 */
    public stopAllSound() {
        if (!ZYSDK.hasDeployed) return;
        this.stopSound(ZYSDK.config.sound.bgm);
        Laya.SoundManager.stopAll();
    }
    /**能否点击音效 */
    public playClickSound() {
        if (!ZYSDK.hasDeployed && !ZYSDK.config.sound.click) return;
        Laya.SoundManager.playSound(this.cookSoundUrlByName(ZYSDK.config.sound.click));
    }
    /** 把音效文件名转换成url */
    private cookSoundUrlByName($name: string) {
        return `${ZYSDK.config.sound.basePath}/${$name}.mp3`;
    }
}
export const soundMgr = sound.instance;